/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.bsn.impl;

import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import org.hutech09.jsale.bsn.ICategoryManager;
import org.hutech09.jsale.bsn.common.DataExistsException;
import org.hutech09.jsale.bsn.common.DataNotExistedException;
import org.hutech09.jsale.bsn.exception.ItemExistsException;
import org.hutech09.jsale.bsn.exception.ItemNotFoundException;
import org.hutech09.jsale.entity.ProductType;
import org.hutech09.jsale.bsn.common.IGenericDao;
import org.hutech09.jsale.entity.Category;

/**
 *
 * @author Nam
 */
@Stateless
public class CategoryManager implements ICategoryManager {

    @EJB
    private IGenericDao dao;

    @Override
    public Category create(Category category) {
        if (category == null) {
            throw new IllegalArgumentException("category can't be null.");
        }
        if (category.getId() != null) {
            throw new IllegalArgumentException("category.id must be null.");
        }
        try {
            return dao.create(category);
        } catch (DataExistsException ex) {
            Logger.getLogger(CategoryManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public void update(Category category) throws ItemExistsException {
        try {
            dao.update(category);
        } catch (DataNotExistedException ex) {
            throw new ItemExistsException(category);
        }
    }

    @Override
    public ProductType create(ProductType productType) {
        if (productType == null) {
            throw new IllegalArgumentException("productType can't be null.");
        }
        if (productType.getId() != null) {
            throw new IllegalArgumentException("productType.id must be null.");
        }
        try {
            return dao.create(productType);
        } catch (DataExistsException ex) {
            Logger.getLogger(CategoryManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public void update(ProductType productType) throws ItemExistsException {
        try {
            dao.update(productType);
        } catch (DataNotExistedException ex) {
            throw new ItemExistsException(productType);
        }
    }

    @Override
    public void deleteCategory(long categoryId) throws ItemNotFoundException {
        try {
            dao.delete(Category.class, categoryId);
        } catch (DataNotExistedException ex) {
            throw new ItemNotFoundException(new Category(categoryId));
        }
    }

    @Override
    public void deleteProductType(long productTypeId) throws ItemNotFoundException {
        try {
            dao.delete(ProductType.class, productTypeId);
        } catch (DataNotExistedException ex) {
            throw new ItemNotFoundException(new ProductType(productTypeId));
        }
    }

    @Override
    public List<Category> getAllCategory() {
        return dao.getAll(Category.class);
    }

    @Override
    public List<ProductType> getAllProductType(Long categoryId) throws ItemNotFoundException {
        Category c = dao.find(Category.class, categoryId);
        if (c == null) {
            throw new ItemNotFoundException(new Category(categoryId));
        }
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put(("category"), c);
        List<ProductType> type = dao.findByNamedQuery("Producttype.findByCategory", map);
        return type;
    }

    @Override
    public List<Category> getall() {
        List<Category> listcate = dao.getAll(Category.class);
        return listcate;
    }

    @Override
    public boolean isCategoryEmpty(Long categoryId) throws ItemNotFoundException {
        Category c = dao.find(Category.class, categoryId);
        if (c == null) {
            throw new ItemNotFoundException(new Category(categoryId));
        }
        for (ProductType type : c.getListProtype()) {
            if (type.getListProduct().size() > 0) {
                return false;
            }
        }
        return true;
    }

    @Override
    public Category getCategory(Long categoryId) {
        return dao.find(Category.class, categoryId);
    }

    @Override
    public boolean isEmpty(ProductType type) {
        if (type == null) {
            throw new IllegalArgumentException("Type can't be null");
        }
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put(("productType"), type);
        List<ProductType> types = dao.findByNamedQuery("Product.findByProductType", map);
        return types.isEmpty();
    }

    @Override
    public List<ProductType> getAllProductType() {
        return dao.getAll(ProductType.class);
    }
}
